<?php
/*
 * the kernel of the framework
 */
$arg=explode('&',trim($_SERVER['QUERY_STRING'], '/'));
if (empty($arg)) {//
    $ARGS=array();
    $ARGS[0]=DEFAULT_CONTROLLER;
    $ARGS[1]=DEFAULT_INDEX;
}else{
    $ARGS=explode('/', $arg[0]);
    if (empty($ARGS[1])) {
        $ARGS[1]=DEFAULT_INDEX;
    }
}

class Frame{
    function __construct(){}
    function run(){
        global $ARGS;
        if(file_exists(CONTROLLER_DIR.$ARGS[0].'.controller.php')){
            include(CONTROLLER_DIR.$ARGS[0].'.controller.php');
            $class_name=ucwords($ARGS[0]).'_Controller';
            $controller=new $class_name();
            if(sizeof($ARGS) > 2){
              call_user_func_array(array($controller,$ARGS[1]),array_slice($ARGS,2));
            }else{
              $controller->$ARGS[1]();
            }
        }else{
            #include(ERROR404);
            return false;
        }
    }
    function __destruct(){}
}

class Model{
    private $plugins;
    function __construct(){
        $this->plugins=array();
    }
    function __get($class_name){
        if (isset($this->plugins[$class_name])) {
            return $this->plugins[$class_name];
        }else{
            #include (ERROR404);
            return false;
        }
    }
    function __call($function_name,$function_vars){
        #include(ERROR404);
    }
    function load_plugin($plugin_name){
        if (isset($this->plugins[$plugin_name])) {
            return true;
        }
        $plugin_path=SYS_PLUGIN_PATH.$plugin_name.'.plugin.php';
        if (file_exists($plugin_path)) {
            include($plugin_path);
            $plugin_class_name=ucwords($plugin_name).'_Plugin';
            $this->plugins[$plugin_name]=new $plugin_class_name();//model ,class Model_Model,model.php
            return true;
        }else{
            $plugin_path=PLUGIN_DIR.$plugin_name.'.plugin.php';
            if (file_exists($plugin_path)) {
                include($plugin_path);
                $plugin_class_name=ucwords($plugin_name).'_Plugin';
                $this->plugins[$plugin_name]=new $plugin_class_name();//model ,class Model_Model,model.php
            }else{
                #include(ERROR404);
                return false;
            }
        }

    }
    function __destruct(){}
}

class View{
    private $args;
    private $layout;
    function __construct(){
        $this->args=array();
        $this->layout = null;
    }
    function __call($function_name,$function_vars){
        #include(ERROR404);
    }
    function assign($var_name,$var_value){
        $this->args[$var_name]=$var_value;
    }
    function v($var_name){
        return isset($this->args[$var_name])?$this->args[$var_name]:null;
    }
    function display($page_info){
        if(is_array($page_info)){
            if(empty($this->layout)){
                $this->layout = new Layout('default');
            }
            foreach($page_info as $block => $widgets){
                $ws = array();
                foreach($widgets as $w){
                    $ws []= new Widget($w);
                    
                }
            }
        }
    }

    function load_layout($layout_name){
        $this->layout = new Layout($layout_name);
    }
    
    function __destruct(){}
}

class Controller{
    private $models;
    private $view;
    function __construct(){
        $this->models=array();
        $this->view=new View();
    }
    function  __get($class_name){
        switch($class_name){
            case 'models':
                return $this->models;
                break;
            case 'view':
                return $this->view;
                break;
            default:
                if (isset($this->models[$class_name])) {
                    return $this->models[$class_name];
                }else{
                    #include(ERROR404);//can't find model
                }
        }
    }
    function  __call($function_name,$function_args){
        #include(ERROR404);
    }
    function load_model($model_name){
        if (isset($this->models[$model_name])) {
            return true;
        }
        $model_path=MODEL_DIR.$model_name.'.model.php';
        if (file_exists($model_path)) {
            include($model_path);
            $model_class_name=ucwords($model_name).'_Model';
            $this->models[$model_name]=new $model_class_name();//model ,class Model,model.model.php
            return true;
        }else{
            #include(ERROR404);
            return false;
        }
    }
    function __destruct(){}
}

class Plugin{
    function __construct(){
    }
    function __get($class_name){
    }
    function __call($function_name,$function_vars){
        #include(ERROR404);
    }
    function __destruct(){}
}

class Widget {
    private $name;
    private $args;
    function __construct($widget_name){
        $this->name = $widget_name;
        $this->args=array();
    }
    function __call($function_name,$function_vars){
        #include(ERROR404);
    }
    function assign($var_name,$var_value){
        $this->args[$var_name]=$var_value;
    }
    function v($var_name){
        return isset($this->args[$var_name])?$this->args[$var_name]:null;
    }
    
    function display(){
        $page_path=VIEW_DIR.$this->name.'.widget.php';
        include($page_path);
    }
    function __destruct(){}
}

class Layout {
    private $layout;
    private $block;
    private $title;
    private $scripts;
    function __construct($layout_name = 'default'){
        $page_path=VIEW_DIR.$layout_name.'.layout.php';
        if(file_exists($page_path)){
            $this->layout = $page_path;
        }else{
            throw new Exception();
        }
        $this->layout = $page_path;
        $this->block = array();
    }
    function __get($class_name){
    }

    function add_widget($widget,$block){
        if(!isset($this->block[$block])){
            $this->block[$block] = array();
        }
        array_push($this->block[$block], $widget);
    }

    function set_title($title){
       $this->title = $title;
    }
    function set_scripts($scripts){
       $this->scripts = $scripts;
    }

    function __call($function_name,$function_vars){
        #include(ERROR404);
        switch ($function_name){
            case 'head':
            case 'left':
            case 'middle':
            case 'right':
            case 'foot':
                if(!empty($this->block[$function_name])){
                    foreach($this->block[$function_name] as $widget){
                        $widget->display();
                    }
                }
                break;
            case 'title':
                echo $this->title;
                break;
            case 'scripts':
                echo $this->scripts;
                break;
        }
    }

    function display(){
        require($this->layout);
    }

    
    function __destruct(){}
}
?>